<?php
namespace addons\fzdc\job;
use app\admin\model\fzdc\order\Order;
use app\admin\model\fzdc\Userdetail;
use think\Log;
use think\queue\Job;

/**
 * 用于处理消息推送
 */
class SendMessage
{
    /**
     * fire是消息队列默认调用的方法
     * @param Job $job 当前的任务对象
     * @param array|mixed $data 发布任务时自定义的数据
     */
    public function fire(Job $job, $data)
    {
        Log::log("进入推送日志队列");
        //执行业务处理
        if($this->doJob($data)){
            $job->delete();//任务执行成功后删除
            Log::log("dismiss job has been down and deleted");
        }else{
            //检查任务重试次数
            if($job->attempts() > 3){
                Log::log("dismiss job has been retried more that 3 times");
                $job->delete();
            }else{
                $job->release(2);
            }
        }
    }

    /**
     * 根据消息中的数据进行实际的业务处理
     */
    private function doJob($data)
    {
        // 实际业务流程处理
        $sendObj = new \addons\fzdc\library\SendMessage;
        Log::info("订阅消息-推送数据：".json_encode($data['data']));
        $result = $sendObj->send_temp($data['openid'],$data['masterplate'],$data['data']);

        if (empty($result)){
            return true;
        }
        if (empty($data)){
            return true;
        }
        $order = Order::get($data['order_id']);
        if(empty($order)){
            return true;
        }
        Log::info("订阅消息-推送结果：".json_encode($result));
        return true;
    }
}